<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form class="verify-form" :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="助记码" prop="mnemonicCode">
      <el-input v-model="dataForm.mnemonicCode" placeholder="药物助记码"></el-input>
    </el-form-item>
    <el-form-item label="药品名称" prop="name">
      <el-input v-model="dataForm.name" placeholder="药品名称"></el-input>
    </el-form-item>
    <el-form-item label="药品产地" prop="production">
      <el-input v-model="dataForm.production" placeholder="药品产地"></el-input>
    </el-form-item>
    <el-form-item label="药品规格" prop="spec">
      <el-input v-model="dataForm.spec" placeholder="药品规格"></el-input>
    </el-form-item>
    <el-form-item label="单位" prop="unit">
      <el-input v-model="dataForm.unit" placeholder="单位"></el-input>
    </el-form-item>
    <el-form-item label="数量/月" prop="metering">
      <el-input v-model="dataForm.metering" placeholder="限制数量/月"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
    import {getHisDrugLimitObj, addHisDrugLimitObj, putHisDrugLimitObj} from '@/api/his/hisdruglimit'

    export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          mnemonicCode: '',
          name: '',
          production: '',
          spec: '',
          unit: '',
          metering: '',
          createTime: '',
          updateTime: '',
          createUser: '',
          modifyUser: '',
        },
        dataRule: {
          mnemonicCode: [
            { required: true, message: '药物助记码不能为空', trigger: 'blur' }
          ],
          name: [
            { required: true, message: '药品名称不能为空', trigger: 'blur' }
          ],
          production: [
            { required: true, message: '药品产地不能为空', trigger: 'blur' }
          ],
          spec: [
            { required: true, message: '药品规格不能为空', trigger: 'blur' }
          ],
          unit: [
            { required: true, message: '单位不能为空', trigger: 'blur' }
          ],
          metering: [
            { required: true, message: '限制数量/月不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ],
          updateTime: [
            { required: true, message: '更新时间不能为空', trigger: 'blur' }
          ],
          createUser: [
            { required: true, message: '创建用户id不能为空', trigger: 'blur' }
          ],
          modifyUser: [
            { required: true, message: '修改用户id不能为空', trigger: 'blur' }
          ],
          delFlag: [
            { required: true, message: '逻辑删除:1-正常,-1删除不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            getHisDrugLimitObj(this.dataForm.id).then(response => {
                this.dataForm = response.data.data
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            if (this.dataForm.id) {
                putHisDrugLimitObj(this.dataForm).then(data => {
                    this.$message.success('修改成功')
                    this.visible = false
                    this.$emit('refreshDataList')
                });
            } else {
                addHisDrugLimitObj(this.dataForm).then(data => {
                    this.$message.success('添加成功')
                    this.visible = false
                    this.$emit('refreshDataList')
                })
            }
          }
        })
      }
    }
  }
</script>
